package cn.fengzhilun.javase.day15;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

import org.junit.Test;

public class TestFile {
	@Test
	public void test1() throws IOException {
		File f = new File("g:/1.txt");
		f.createNewFile();

		// f.delete();

		f.renameTo(new File("g:/2.txt"));
		System.out.println(f.getName());
	}

	@Test
	public void test2() throws IOException {
		File f = new File("g:/工程大学视频");
		// bianLi(f);
		bianLi1(f);
		System.out.println(fb(6));
	}

	public static void bianLi(File f) {
		// System.out.println(f);
		File[] files = f.listFiles();

		for (File f1 : files) {
			System.out.println(f1);
			if (f1.isDirectory()) {
				bianLi(f1);
			}
		}
	}

	// 斐波那契
	public int fb(int num) {
		if (num == 1 || num == 2) {
			return 1;
		}
		return fb(num - 1) + fb(num - 2);

	}

	public static void bianLi1(File f) throws IOException {
		// 第一个参数是从哪里遍历。
		Files.walkFileTree(f.toPath(), new FileVisitor<Path>() {

			@Override
			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				System.out.println(file);

				System.out.println(attrs.size());
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
				// TODOAuto-generated method stub
				return FileVisitResult.CONTINUE;

			}

			@Override
			public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
				// TODO Auto-generated method stub
				return FileVisitResult.CONTINUE;
			}
		});
	}
}
